#include "imageviewer.h"

#include <stdio.h>
#include <iostream>

#include <QPainter.h>

/******************************************************/
ImageViewer::ImageViewer(const QString& title):
QWidget()
{
	setWindowTitle(title);	
}

/******************************************************/
ImageViewer::~ImageViewer()
{}

/******************************************************/
void ImageViewer::setImage(QImage* image)
{
	_image = image;
	resize(_image->width(), _image->height());

	show();
}

/******************************************************/
void ImageViewer::paintEvent(QPaintEvent* evt)
{
	QPainter painter(this);

	if (_image)
		painter.drawImage(QPoint(0,0), *_image);

	for (unsigned int i=0; i < _points.size(); ++i)
	{
		const double cross_len = _points[i]._cross_len;
		painter.setPen(_points[i]._colour);
		QPoint p1, p2, p3, p4;
		p1 = _points[i]._point + QPoint(cross_len,0);
		p2 = _points[i]._point + QPoint(-cross_len,0);
		p3 = _points[i]._point + QPoint(0, cross_len);
		p4 = _points[i]._point + QPoint(0, -cross_len);
		painter.drawLine(p1, p2);
		painter.drawLine(p3, p4);
	}

	for (unsigned int i=0; i < _lines.size(); ++i)
	{
		painter.setPen(_lines[i]._colour);
		painter.drawLine(_lines[i]._start,_lines[i]._end);
	}

	for (unsigned int i=0; i < _circles.size(); ++i)
	{
		painter.setPen(_circles[i]._colour);
		painter.drawEllipse(_circles[i]._centre,(int)_circles[i]._radius,(int)_circles[i]._radius);
	}

	for (unsigned int i=0; i < _text.size(); ++i)
	{
		painter.setPen(_text[i]._colour);
		painter.setFont(QFont("Arial", _text[i]._size));
		painter.drawText(_text[i]._top_left, _text[i]._text);
	}

	QWidget::paintEvent(evt);
}

/******************************************************/
void ImageViewer::drawLine(const QPoint& start, const QPoint& end, const QColor& c)
{
	Line line;
	line._start = start;
	line._end = end;
	line._colour = c;

	_lines.push_back(line);
	show();
}

/******************************************************/
void ImageViewer::drawPoint(const QPoint& point, const double cross_len, const QColor& c)
{
	Point pt;
	pt._point = point;
	pt._cross_len = cross_len;
	pt._colour = c;

	_points.push_back(pt);
	show();
}

/******************************************************/
void ImageViewer::drawCircle(const QPoint& centre, double radius, const QColor& c)
{
	Circle circle;
	circle._centre = centre;
	circle._radius = radius;
	circle._colour = c;

	_circles.push_back(circle);
	show();
}

/******************************************************/
void ImageViewer::drawText(const QPoint& top_left, const QString& text, double size, const QColor& c)
{
	Text txt;
	txt._top_left = top_left;
	txt._text = text;
	txt._size = size;
	txt._colour = c;

	_text.push_back(txt);
	show();
}
	
/******************************************************/
void ImageViewer::clearOverlay()
{
	_lines.clear();
	_points.clear();
	_circles.clear();
	_text.clear();
	show();
}

/******************************************************/
void ImageViewer::clearImage()
{
	_image = 0;
	show();
}